package com.sykj.jiangpengcheng.utils;

import com.sykj.utils.ResultVO;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 李丛旺
 * @description
 * @date 2020/7/15
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {


    @ExceptionHandler(APIException.class)
    public ResultVO APIExceptionHandler(APIException e) {
        // 注意哦，这里传递的响应码枚举
        return new ResultVO(ResultCode.FAILED, e.getMsg());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 注意哦，这里传递的响应码枚举
        return new ResultVO(ResultCode.VALIDATE_FAILED, objectError.getDefaultMessage());
    }

}
